home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3169 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Basic Question on SWITCH
  5. Date: Fri, 26 Jan 1996 17:03:37 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <3108ED49.987@cmt.lpr.mail.carel.fi>
  8. References: <4e4cu4$95f@vixen.cso.uiuc.edu> <4e8p6m$n8q@ns.RezoNet.NET>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Ray Dunn wrote:
  16. > In referenced article, HOTARD says...
  17. > >I am just learning how to program in C, and I had a question about
  18. > >switch.[snip]
  19. > No, the only thing you can do is to give a series of different case
  20. > labels, like:
  21. >   switch (character)
  22. >   {
  23. >    case '0':
  24. >    case '1':
  25. >    case '2':
  26. >    [etc]
  27. >    case '9':
  28. >     [process a digit here]
  29. >     break;
  30. >    case 'a':
  31. >    case 'b':
  32. >    etc.
  33. >    default:
  34. >      [none of the above]
  35. > A switch is not a good choice for what you're trying to do - although
  36. > it might produce quite efficient code, the source is very verbose.[snip]
  37.  
  38. If the task is just to test for a, say, digit (eg. char a = 'X'; is the character 
  39. in a a digit?), then you could also use:
  40.  
  41.     if (strchr("0123456789", a))
  42.         ... /* It was a digit, do something */
  43.  
  44. With proper optimizer (inline intrinsinct) this would produce quite efficient 
  45. code, too.
  46.  
  47. Later,
  48. AriL
  49. -- 
  50. All my opinions are mine and mine alone.
  51.